package com.e3.basdoc.region.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.e3.basdoc.region.dao.RegionDAO;
import com.e3.basdoc.region.entity.RegionEntity;
import com.e3.basdoc.region.model.RegionModel;
import com.e3.basdoc.region.model.RegionQC;
import com.e3.basdoc.region.service.RegionCacheManager;
import com.e3.basdoc.region.service.RegionQueryService;
import com.e3.core.base.DAOHelper;
import com.e3.core.base.ServiceException;
import com.e3.core.base.SuperQueryService;

/**
 * Description: <br>
 * 
 * @author: <a href="mailto:senton1101@126.com">TianXiangdong</a> 2010-8-6
 */
public class RegionQueryServiceImpl extends SuperQueryService implements RegionQueryService {

	private RegionDAO regionDAO;
	
	private RegionDAO getRegionDAO() {
		if (regionDAO == null) {
			regionDAO = DAOHelper.createDAOInstance(RegionDAO.class);
		}
		return regionDAO;
	}

	// 根据code获取RegionModel
	/*
	 * (non-Javadoc)
	 * @see com.e3.basdoc.region.service.impl.RegionQueryService#getRegionModelByCode(java.lang.String)
	 */
	public RegionModel getRegionModelByCode(String code) throws ServiceException {
		RegionEntity region = getRegionDAO().getRegionByCode(code);
		return regionEntityToModel(region);
	}

	// 根据id获取RegionModel
	/*
	 * (non-Javadoc)
	 * @see com.e3.basdoc.region.service.impl.RegionQueryService#getRegionModelById(long)
	 */
	public RegionModel getRegionModelById(long id) throws ServiceException {
		RegionModel model = RegionCacheManager.INSTANCE.get(id);
		if(model!=null){
			return model;
		}
		
		RegionEntity region = getRegionDAO().getById(RegionEntity.class, id);
		if(region == null){
			return null;
		}
		RegionModel rModel = regionEntityToModel(region);
		if(rModel!=null){
			RegionCacheManager.INSTANCE.put(rModel);
		}
		
		return rModel;
	}


	// 根据code列表获取RegionModel
	/*
	 * (non-Javadoc)
	 * @see com.e3.basdoc.region.service.impl.RegionQueryService#getRegionModelByCodes(java.lang.String)
	 */
	public List<RegionModel> getRegionModelByCodes(String... codes) throws ServiceException {
		List<RegionEntity> regionEntities = getRegionDAO().findBySingleField(RegionEntity.class, "code", codes);

		if (regionEntities != null && regionEntities.size() > 0) {
			List<RegionModel> regionModels = new ArrayList<RegionModel>(regionEntities.size());
			for (RegionEntity regionEntity : regionEntities) {
				regionModels.add(regionEntityToModel(regionEntity));
			}
			return regionModels;
		}

		return null;
	}

	// 根据id列表获取RegionModel
	/*
	 * (non-Javadoc)
	 * @see com.e3.basdoc.region.service.impl.RegionQueryService#getRegionModelByIds(java.lang.Long)
	 */
	public List<RegionModel> getRegionModelByIds(Long... ids) throws ServiceException {
		if(ids==null||ids.length==0){
			return null;
		}
		List<RegionModel> models = new ArrayList<RegionModel>(ids.length);
		for (Long id : ids) {
			RegionModel model = getRegionModelById(id);
			models.add(model);
		}
		return models;

	}

	// 根据查询条件regionQC获取List<RegionModel>
	/*
	 * (non-Javadoc)
	 * @see com.e3.basdoc.region.service.impl.RegionQueryService#getRegionByCondition(com.e3.basdoc.region.model.RegionQC)
	 */
	public List<RegionModel> getRegionByCondition(RegionQC regionQC) throws ServiceException {
		
		List<Long> ids = getRegionDAO().getRegionIdsByCondition(regionQC);
		if(ids!=null&&ids.size()>0){
			Long[] idArray = new Long[ids.size()];
			ids.toArray(idArray);
			return getRegionModelByIds(idArray);
		}
		return null;

	}

	// 获取参数regionModel的子regionModel
	/*
	 * (non-Javadoc)
	 * @see com.e3.basdoc.region.service.impl.RegionQueryService#getRegionChildren(com.e3.basdoc.region.model.RegionModel)
	 */
	public List<RegionModel> getRegionChildren(RegionModel regionModel) throws ServiceException {
		long id = 0;
		if(regionModel!=null){
			id = regionModel.getId();
		}
		List<Long> ids = getRegionDAO().getChildrenIds(new Long[]{id});
		Long[] idArray = new Long[ids.size()];
		ids.toArray(idArray);
		return getRegionModelByIds(idArray);
		
	}

	/*
	 * (non-Javadoc)
	 * @see com.e3.basdoc.region.service.impl.RegionQueryService#getAllRegions()
	 */
	public List<RegionModel> getAllRegions() throws ServiceException {
		return getRegionDAO().getAllRegions();
	}

	/*
	 * (non-Javadoc)
	 * @see com.e3.basdoc.region.service.impl.RegionQueryService#getRegionChildrenSize(com.e3.basdoc.region.model.RegionModel)
	 */
	public long getRegionChildrenSize(RegionModel regionModel) throws ServiceException {
		long id = regionModel.getId();
		return getRegionDAO().childrenCount(id);
	}

	private RegionModel regionEntityToModel(RegionEntity region) {
		RegionModel regionModel = new RegionModel();
		regionModel = super.entityToModel(regionModel, region);

		regionModel.setId(region.getId());
		regionModel.setParentId(region.getParentId());
		regionModel.setCode(region.getCode());
		regionModel.setName(region.getName());
		regionModel.setEnglishName(region.getEnglishName());
		regionModel.setZipcode(region.getZipcode());

		return regionModel;
	}

	public List<RegionModel> getPagingRegionModels(RegionQC qc,int start, int max) throws ServiceException {
		List<Long> ids = getRegionDAO().getPagingRegionIdsByCondition(qc,start,max);
		if(ids!=null&&ids.size()>0){
			Long[] idArray = new Long[ids.size()];
			ids.toArray(idArray);
			return getRegionModelByIds(idArray);
		}
		return null;
	}

	public long getRegionSizeByCondition(RegionQC qc) throws ServiceException {
		return getRegionDAO().counterByCondition(qc);
	}

}
